﻿/*************************************************************************
* @File			: Logger.cs
* @Description	: 日志
*
*
*
*
************************************************************************/
using System;
using System.IO;
using System.Text;
using UnityEngine;

namespace Framework
{

    public class Logger
    {
        // @brief 【日志默认】是否开启
        public static bool ENABLE_NORMAL = false;
        // @brief 【日志警告】是否开启
        public static bool ENABLE_WARNING = false;
        // @brief 【日志错误】是否开启
        public static bool ENABLE_ERROR = true;
        // @brief 【日志系统】是否开启
        public static bool ENABLE_SYS = true;
        // @brief 【日志颜色】是否开启
        public static bool ENABLE_COLOR = true;
        // @brief 【日志Lua错误】是否开启
        public static bool ENABLE_LUATRACE = true;
        // @brief 记录
        public static LogRecord ms_kRec = new LogRecord();
        // @brief 最大缓存字符串
        private static StringBuilder ms_sbTmp0 = new StringBuilder(512);
        // @brief 写出lua错误文件会自动生成
        private static string m_luaFile = null;
        // -----------------------------------------------------------------------
        // @brief 设置是否显示日志
        static public void SetShowLog(bool show, bool luatrace)
        {
            if (show)
            {
                ENABLE_NORMAL = true;
                ENABLE_WARNING = true;
                ENABLE_ERROR = true;
                ENABLE_SYS = true;
                ENABLE_COLOR = true;
            }
            else
            {
                ENABLE_NORMAL = false;
                ENABLE_WARNING = false;
                ENABLE_ERROR = true;
                ENABLE_SYS = true;
                ENABLE_COLOR = false;
            }
            ENABLE_LUATRACE = luatrace;
        }
        // -----------------------------------------------------------------------
        // @brief 初始化
        public static void Init(bool init)
        {
            if (!init)
                return;
            ms_kRec.Init();
        }
        // -----------------------------------------------------------------------
        // @brief 日志之
        static public void Log(params string[] logs)
        {
            if (logs == null || logs.Length <= 0)
                return;
            ms_sbTmp0.Length = 0;
            foreach (string log in logs)
                ms_sbTmp0.Append(log);
            Logger.Log(ms_sbTmp0.ToString());
            ms_sbTmp0.Length = 0;
        }
        // -----------------------------------------------------------------------
        // @brief 日志之
        public static void Log(string msg)
        {
            if (!ENABLE_NORMAL)
                return;
            UnityEngine.Debug.Log(msg);
            ms_kRec.Log(msg);
        }
        // -----------------------------------------------------------------------
        // @brief 日志之【错误】
        public static void LogError(string msg, bool bStackInfo = true)
        {
            if (!ENABLE_ERROR)
                return;
            UnityEngine.Debug.LogError(msg);
            ms_kRec.LogError(msg);
            if (bStackInfo)
            {
                string strStack = StackTraceUtility.ExtractStackTrace();
                ms_kRec.LogError(strStack);
            }
        }
        // -----------------------------------------------------------------------
        // @brief 日志之【错误】
        public static void LogError(string msg, Exception ex)
        {
            if (!ENABLE_ERROR)
                return;
            LogError(msg + "\nError: " + ex.Message + "\n" + ex.StackTrace, false);
        }
        // -----------------------------------------------------------------------
        // @brief 日志之【警告】
        public static void LogWarn(string msg)
        {
            if (!ENABLE_WARNING)
                return;
            UnityEngine.Debug.LogWarning(msg);
            ms_kRec.LogWarn(msg);
        }
        // -----------------------------------------------------------------------
        // @brief 日志之【系统】
        public static void LogSys(string msg, bool bStackInfo = false)
        {
            if (!ENABLE_SYS)
                return;
            UnityEngine.Debug.Log(msg);
            ms_kRec.LogSys(msg);
            if (bStackInfo)
            {
                string strStack = StackTraceUtility.ExtractStackTrace();
                ms_kRec.LogSys(strStack);
            }
        }
        // -----------------------------------------------------------------------
        // @brief 日志之【颜色】
        public static void LogColor(string msg, string strColor = "#00FFFF", bool bStackInfo = false)
        {
            if (!ENABLE_COLOR)
                return;
            ms_sbTmp0.Length = 0;
            ms_sbTmp0.AppendFormat("<color={0}>{1}</color>", strColor, msg);
            UnityEngine.Debug.Log(ms_sbTmp0.ToString());
            ms_kRec.LogColor(msg);
            if (bStackInfo)
            {
                string strStack = StackTraceUtility.ExtractStackTrace();
                ms_kRec.LogColor(strStack);
            }
        }
        // -----------------------------------------------------------------------
        // @brief 日志之【Lua错误】
        public static void LogLuaTrace(string msg)
        {
            if (!ENABLE_LUATRACE || Application.platform != RuntimePlatform.WindowsPlayer)
                return;
            string[] formatStrs = new string[] { "[Trace：", DateTime.Now.ToString("HH:mm:ss"), "]", msg, "\r\n" };

            try
            {
                if (string.IsNullOrEmpty(m_luaFile))
                {
                    DateTime now = DateTime.Now;
                    m_luaFile = Application.dataPath + "/logs/lua_trace_" + (now.ToString("yyyy-MM-dd-HH-mm-ss")) + ".log";
                    string dir = Path.GetDirectoryName(m_luaFile);
                    if (!Directory.Exists(dir))
                    {
                        Directory.CreateDirectory(dir);
                    }
                }

                File.WriteAllText(m_luaFile, string.Concat(formatStrs));
            }
            catch (Exception)
            {
            }
        }
    }
}